package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.HealthStringUtils;
import com.itheima.utils.MD5Utils;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wgq
 * @date 2019-10-19 19:40
 * 用户业务层，包含用户注册会员和用户登录
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    /**
     * 根据手机号判断，用户是否已经注册为会员
     *
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    /**
     * 注册新用户成为会员
     *
     * @param member
     */
    @Override
    public void add(Member member) throws Exception {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        } else {
            member.setPassword(MD5Utils.md5("123456"));
            //短信通知用户的初始默认密码为  123456
            //SMSUtils.sendShortMessage(SMSUtils.PASSWORD_CODE,member.getPhoneNumber(),"123456");
        }
        memberDao.add(member);
    }

    /**
     * 会员统计 -- > 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage == null || currentPage <= 0) {
            currentPage = 1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        String queryString = queryPageBean.getQueryString();
        if (HealthStringUtils.isEmpty(queryString)) {
            queryString = "";
        } else {
            queryString = queryString.trim();
        }
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> page = memberDao.selectByCondition("%" + queryString + "%");
        long total = page.getTotal();
        List<Member> result = page.getResult();
        //SELECT * FROM t_checkitem LIMIT 1,10
        return new PageResult(total, result);
    }

    /**
     * 统计分析 --> 会员数量 --> 根据月份查询 会员数量
     * @param
     * @return
     */
    @Override
    public Map findMemberCountByMonth() {
        Calendar calendar = Calendar.getInstance();
        //获取当前日期之前12 个月的日期
        calendar.add(Calendar.MONTH, -12);
        List<String> months = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            months.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
        }
        Map map = new HashMap<>();
        map.put("months", months);//这里要一个 list 集合
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            month = month + ".31";// 格式：2019.3.21
            Integer count = memberDao.findMemberCountBeforeDate(month);
            memberCount.add(count);
        }
        map.put("memberCount", memberCount);
        return map;
    }
}
